package com.taomu.app.zongchouhuli.utils.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.utils.entity.SelectBalanceEntity;

import java.util.ArrayList;
import java.util.List;

public class SelectBalanceAdp extends RecyclerView.Adapter<SelectBalanceAdp.SelectBalanceHolder> {

    private List<SelectBalanceEntity> mData;

    private List<SelectBalanceEntity> mSelectData;

    private Context mContext;

    private LayoutInflater inflater;

    public SelectBalanceAdp(Context mContext){
        this.mContext = mContext;
        this.mData = new ArrayList<>();
        this.mSelectData = new ArrayList<>();
        this.inflater = LayoutInflater.from(mContext);
    }

    public void setData(List<SelectBalanceEntity> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    public void setData(List<SelectBalanceEntity> mData,SelectBalanceEntity data){
        this.mData.clear();
        this.mData.addAll(mData);
        if(data != null){
            mSelectData.clear();
            mSelectData.add(data);
        }
        notifyDataSetChanged();
    }

    public void addData(List<SelectBalanceEntity> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public SelectBalanceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_dialog_select_balance,parent,false);
        return new SelectBalanceHolder(view);
    }

    @Override
    public void onBindViewHolder(SelectBalanceHolder holder, int position) {
        final SelectBalanceEntity entity = mData.get(position);
        holder.update(entity);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkSelect(entity);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class SelectBalanceHolder extends RecyclerView.ViewHolder{

        private CheckBox mCheckBox;

        private TextView mBankNameTv;

        public SelectBalanceHolder(View itemView) {
            super(itemView);
            mCheckBox = itemView.findViewById(R.id.checkbox);
            mBankNameTv = itemView.findViewById(R.id.item_dialog_select_balance_tv);
        }

        public void update(SelectBalanceEntity entity){
            if(isSelect(entity)){
                mCheckBox.setVisibility(View.VISIBLE);
                itemView.setBackgroundColor(mContext.getResources().getColor(R.color.trans_blue));
                mBankNameTv.setTextColor(mContext.getResources().getColor(R.color.base_blue));
            }else{
                itemView.setBackgroundColor(mContext.getResources().getColor(R.color.base_white));
                mCheckBox.setVisibility(View.GONE);
                mBankNameTv.setTextColor(mContext.getResources().getColor(R.color.text_normal_black));
            }
            mBankNameTv.setText(entity.getTitle());
        }
    }

    public void checkSelect(SelectBalanceEntity entity){
        mSelectData.clear();
        mSelectData.add(entity);
        notifyDataSetChanged();
    }

    public boolean isSelect(SelectBalanceEntity entity){
        return mSelectData.contains(entity);
    }

    public List<SelectBalanceEntity> getSelectData(){
        return mSelectData;
    }
}
